package org.csu.myPetStore.persistence.impl;

import org.csu.myPetStore.domain.CloseSource;
import org.csu.myPetStore.persistence.DBUtil;
import org.csu.myPetStore.persistence.HotDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class HotDAOImpl implements HotDAO {

    private static final String getHotGoods = "SELECT * FROM productsession";
    private static final String updateGoodsCount = "UPDATE productsession SET count = ? WHERE productname = ?";
    private static final String getGoodsCount = "SELECT count FROM productsession WHERE productname = ?";
    private static final String insertGoods = "INSERT INTO productsession(productname,count) VALUES(?,?)";
    private static final String updateGoodsByUser = "SELECT * FROM cart WHERE username = ?";

    private List<Integer> list = new ArrayList<Integer>();
    private String HotProductName = null;

    @Override
    public String getHotGoods() {
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(getHotGoods);
            ResultSet resultSet = preparedStatement.executeQuery();

            int countTem = 0;

            while (resultSet.next()){
                String productname = resultSet.getString(1);
                int count = Integer.parseInt(resultSet.getString(2));

                if(count > countTem){
                    countTem = count;
                    HotProductName = productname;
                }
            }

            CloseSource closeSource = new CloseSource();
            closeSource.closeSource(connection,preparedStatement,resultSet,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  HotProductName;
    }

    public void updateGoodsCount(String goods, int count,Connection connection) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(updateGoodsCount);
            preparedStatement.setInt(1,count);
            preparedStatement.setString(2,goods);
            int result = preparedStatement.executeUpdate();
            if(result == 1)
                System.out.println("更新goods成功");

            CloseSource closeSource = new CloseSource();
            closeSource.closeSource(null,preparedStatement,null,null);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getGoodsCount(String goods) {
        int num = 0;
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(getGoodsCount);
            preparedStatement.setString(1,goods);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                num = resultSet.getInt(1);
            }
            CloseSource closeSource = new CloseSource();
            closeSource.closeSource(connection,preparedStatement,resultSet,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }

    public void insertGoods(String goods, int count,Connection connection) {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(insertGoods);
            preparedStatement.setString(1,goods);
            preparedStatement.setInt(2,count);

            int result = preparedStatement.executeUpdate();
            if(result == 1)
                System.out.println("插入goods成功");

            CloseSource closeSource = new CloseSource();
            closeSource.closeSource(null,preparedStatement,null,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateGoodsByUser(String username) {
        try {
            CloseSource closeSource = new CloseSource();
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(updateGoodsByUser);
            preparedStatement.setString(1,username);
            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()){
                String goods = resultSet.getString(2);
                int count = resultSet.getInt(3);        //新购买的数目

                PreparedStatement preparedStatement1 = connection.prepareStatement(getGoodsCount);
                preparedStatement1.setString(1,goods);

                ResultSet resultSet1 = preparedStatement1.executeQuery();
                if(resultSet1.next()){     //如果已经有了，则更新数目，否则插入
                    int num = resultSet1.getInt(1);
                    updateGoodsCount(goods,count+num,connection);
                }else{
                    insertGoods(goods,count,connection);
                }
                closeSource.closeSource(null,preparedStatement1,resultSet1,null);
            }

            closeSource.closeSource(connection,preparedStatement,resultSet,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
